<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>自制深度学习框架--张量 | kiloGrand</title><meta name="keywords" content="kuiper_infer"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本文以arma::cube为基础实现了Tensor类，提供了更方便的访问方式和对外接口。 Tensor类模板C++类模板例子：12345678910111213141516171819202122232425&#x2F;&#x2F; 类模板template &lt;typename T1, typename T2&gt; &#x2F;&#x2F; 或者template&lt;class T1, class T2&gt;class Com">
<meta property="og:type" content="article">
<meta property="og:title" content="自制深度学习框架--张量">
<meta property="og:url" content="https://kilogrand.gitee.io/2023/03/13/kuiper_infer-L2/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="本文以arma::cube为基础实现了Tensor类，提供了更方便的访问方式和对外接口。 Tensor类模板C++类模板例子：12345678910111213141516171819202122232425&#x2F;&#x2F; 类模板template &lt;typename T1, typename T2&gt; &#x2F;&#x2F; 或者template&lt;class T1, class T2&gt;class Com">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2023-03-13T01:41:10.000Z">
<meta property="article:modified_time" content="2023-03-14T15:14:41.649Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="kuiper_infer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2023/03/13/kuiper_infer-L2/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '自制深度学习框架--张量',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-14 23:14:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">自制深度学习框架--张量</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-13T01:41:10.000Z" title="发表于 2023-03-13 09:41:10">2023-03-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-14T15:14:41.649Z" title="更新于 2023-03-14 23:14:41">2023-03-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="自制深度学习框架--张量"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>本文以arma::cube为基础实现了Tensor类，提供了更方便的访问方式和对外接口。</p>
<h2 id="Tensor类模板"><a href="#Tensor类模板" class="headerlink" title="Tensor类模板"></a>Tensor类模板</h2><p>C++类模板例子：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 类模板</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T1, <span class="keyword">typename</span> T2&gt; <span class="comment">// 或者template&lt;class T1, class T2&gt;</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Complex</span> &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Complex</span>(T1 a, T2 b) : _a(a), _b(b);</span><br><span class="line">    Complex&lt;T1, T2&gt; <span class="keyword">operator</span>+(Complex&lt;T1, T2&gt; &amp;c);</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    T1 _a;</span><br><span class="line">    T2 _b;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类模板-全特化（具体化）</span></span><br><span class="line"><span class="keyword">template</span> &lt;&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Complex</span>&lt;<span class="type">int</span>, <span class="type">int</span>&gt; &#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Complex</span>(<span class="type">int</span> a, <span class="type">int</span> b) : _a(a), _b(b);</span><br><span class="line">    Complex&lt;<span class="type">int</span>, <span class="type">int</span>&gt; <span class="keyword">operator</span>+(Complex&lt;<span class="type">int</span>, <span class="type">int</span>&gt; &amp;c);</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> _a;</span><br><span class="line">    <span class="type">int</span> _b;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function">Complex&lt;<span class="type">int</span>, <span class="type">int</span>&gt; <span class="title">c1</span><span class="params">(<span class="number">1</span>,<span class="number">2</span>)</span></span>;</span><br></pre></td></tr></table></figure><br>模板分为类模板与函数模板，特化分为全特化与偏特化（partial specialization）。 </p>
<p>对于模板、模板的全特化和模板的偏特化, 以及同名普通函数都存在的情况下，编译器在编译阶段进行匹配时，只匹配普通函数和模板, 匹配顺序如下:</p>
<ol>
<li>查找普通函数中有没有匹配的,如果有就选它</li>
<li>查找模板中有没有匹配的, 并选则最匹配的版本, 然后进行下面两步</li>
</ol>
<p>注意, 上面规则没提到特化版本, 如果编译器匹配到了规则2, 然后才进行特化版本的匹配</p>
<ol>
<li>查找全特化版本中有没有匹配的</li>
<li>查找偏特化版本中有没有匹配的</li>
</ol>
<p>Tensor共有两个类型，一个类型是Tensor<float>，另一个类型是Tensor<uint8_t>, Tensor<uint8_t> 可能会在后续的量化课程中进行使用，目前还暂时未实现.<br>我们把Tensor<float>和Tensor<uint8_t>全特化，如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tensor</span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tensor</span>&lt;<span class="type">uint8_t</span>&gt; &#123;</span><br><span class="line">  <span class="comment">// 待实现</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tensor</span>&lt;<span class="type">float</span>&gt; &#123;</span><br><span class="line">  <span class="comment">// 待实现</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="张量"><a href="#张量" class="headerlink" title="张量"></a>张量</h2><p><a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/cpp-const-keyword.html">const小结</a><br><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/66eb9078757b">常量引用</a></p>
<ul>
<li>const修饰实参：表示不能改变实参的值</li>
<li>const成员函数：表示不能改变所有成员变量的值</li>
<li>常量引用：不能通过引用修改其所绑定的对象，但能以其它方式修改这个对象。</li>
</ul>
<h3 id="构造函数"><a href="#构造函数" class="headerlink" title="构造函数"></a>构造函数</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 默认</span></span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">Tensor</span><span class="params">()</span> </span>= <span class="keyword">default</span>;</span><br><span class="line">  <span class="comment">// 参数构造</span></span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">Tensor</span><span class="params">(<span class="type">uint32_t</span> channels, <span class="type">uint32_t</span> rows, <span class="type">uint32_t</span> cols)</span></span>;</span><br><span class="line">  <span class="comment">// 拷贝构造</span></span><br><span class="line">  <span class="built_in">Tensor</span>(<span class="type">const</span> Tensor &amp;tensor);</span><br><span class="line">  <span class="comment">// 赋值构造</span></span><br><span class="line">  Tensor&lt;<span class="type">float</span>&gt; &amp;<span class="keyword">operator</span>=(<span class="type">const</span> Tensor &amp;tensor);</span><br><span class="line"></span><br><span class="line">Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Tensor</span>(<span class="type">uint32_t</span> channels, <span class="type">uint32_t</span> rows, <span class="type">uint32_t</span> cols) &#123;</span><br><span class="line">  data_ = arma::<span class="built_in">fcube</span>(rows, cols, channels);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Tensor</span>(<span class="type">const</span> Tensor &amp;tensor) &#123;</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_ = tensor.data_;</span><br><span class="line">  <span class="keyword">this</span>-&gt;raw_shapes_ = tensor.raw_shapes_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Tensor&lt;<span class="type">float</span>&gt; &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="keyword">operator</span>=(<span class="type">const</span> Tensor &amp;tensor) &#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">this</span> != &amp;tensor) &#123;</span><br><span class="line">    <span class="keyword">this</span>-&gt;data_ = tensor.data_;</span><br><span class="line">    <span class="keyword">this</span>-&gt;raw_shapes_ = tensor.raw_shapes_;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="张量的维度大小"><a href="#张量的维度大小" class="headerlink" title="张量的维度大小"></a>张量的维度大小</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 返回行数</span></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">rows</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="comment">// 返回列数</span></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">cols</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="comment">// 返回通道数</span></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">channels</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="comment">// 返回张量中元素的个数</span></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">size</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="comment">// 返回每个维度大小</span></span><br><span class="line">  <span class="function">std::vector&lt;<span class="type">uint32_t</span>&gt; <span class="title">shapes</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="type">uint32_t</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">rows</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.n_rows;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">uint32_t</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">cols</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.n_cols;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">uint32_t</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">channels</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.n_slices;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">uint32_t</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">size</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">size</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">std::vector&lt;<span class="type">uint32_t</span>&gt; Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">shapes</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">return</span> &#123;<span class="keyword">this</span>-&gt;<span class="built_in">channels</span>(), <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>(), <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>()&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="取数据和索引"><a href="#取数据和索引" class="headerlink" title="取数据和索引"></a>取数据和索引</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 返回data</span></span><br><span class="line">  <span class="function">arma::fcube &amp;<span class="title">data</span><span class="params">()</span></span>;</span><br><span class="line">  <span class="function"><span class="type">const</span> arma::fcube &amp;<span class="title">data</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 返回某一通道的数据</span></span><br><span class="line">  <span class="function">arma::fmat &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel)</span></span>;</span><br><span class="line">  <span class="function"><span class="type">const</span> arma::fmat &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel)</span> <span class="type">const</span></span>;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 索引，返回data[channel, row, col]</span></span><br><span class="line">  <span class="function"><span class="type">float</span> <span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col)</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="function"><span class="type">float</span> &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 返回 第channel * Rows * Cols + row * Cols + col 个数据，相当于展平为1维后的索引</span></span><br><span class="line">  <span class="function"><span class="type">float</span> <span class="title">index</span><span class="params">(<span class="type">uint32_t</span> offset)</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">arma::fcube &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">data</span>() &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">const</span> arma::fcube &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">data</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">arma::fmat &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">at</span>(<span class="type">uint32_t</span> channel) &#123;</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(channel, <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">slice</span>(channel);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">const</span> arma::fmat &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">at</span>(<span class="type">uint32_t</span> channel) <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(channel, <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">slice</span>(channel);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">float</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">at</span>(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col) <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(row, <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>());</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(col, <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>());</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(channel, <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">at</span>(row, col, channel);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">float</span> &amp;Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">at</span>(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col) &#123;</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(row, <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>());</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(col, <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>());</span><br><span class="line">  <span class="built_in">CHECK_LT</span>(channel, <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">at</span>(row, col, channel);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">float</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">index</span>(<span class="type">uint32_t</span> offset) <span class="type">const</span> &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(offset &lt; <span class="keyword">this</span>-&gt;data_.<span class="built_in">size</span>());</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">at</span>(offset);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="初始化张量"><a href="#初始化张量" class="headerlink" title="初始化张量"></a>初始化张量</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 赋值</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">set_data</span><span class="params">(<span class="type">const</span> arma::fcube &amp;data)</span></span>;</span><br><span class="line">  <span class="comment">// 全为1</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Ones</span><span class="params">()</span></span>;</span><br><span class="line">  <span class="comment">// 随机</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Rand</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">set_data</span>(<span class="type">const</span> arma::fcube &amp;data) &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(data.n_rows == <span class="keyword">this</span>-&gt;data_.n_rows) &lt;&lt; data.n_rows &lt;&lt; <span class="string">&quot; != &quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;data_.n_rows;</span><br><span class="line">  <span class="built_in">CHECK</span>(data.n_cols == <span class="keyword">this</span>-&gt;data_.n_cols) &lt;&lt; data.n_cols &lt;&lt; <span class="string">&quot; != &quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;data_.n_cols;</span><br><span class="line">  <span class="built_in">CHECK</span>(data.n_slices == <span class="keyword">this</span>-&gt;data_.n_slices) &lt;&lt; data.n_slices &lt;&lt; <span class="string">&quot; != &quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;data_.n_slices;</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_ = data;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Rand</span>() &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">randn</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Ones</span>() &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">fill</span>(<span class="number">1.</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="张量填充"><a href="#张量填充" class="headerlink" title="张量填充"></a>张量填充</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 边界填充</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Padding</span><span class="params">(<span class="type">const</span> std::vector&lt;<span class="type">uint32_t</span>&gt; &amp;pads, <span class="type">float</span> padding_value)</span></span>;</span><br><span class="line">  <span class="comment">// 用标量值填充</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Fill</span><span class="params">(<span class="type">float</span> value)</span></span>;</span><br><span class="line">  <span class="comment">// 用vector填充</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Fill</span><span class="params">(<span class="type">const</span> std::vector&lt;<span class="type">float</span>&gt; &amp;values)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Padding</span>(<span class="type">const</span> std::vector&lt;<span class="type">uint32_t</span>&gt; &amp;pads, <span class="type">float</span> padding_value) &#123;</span><br><span class="line">  <span class="comment">// Usage: tensor.Padding(&#123;1, 1, 1, 1&#125;, 0); // 边缘填充为0</span></span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="built_in">CHECK_EQ</span>(pads.<span class="built_in">size</span>(), <span class="number">4</span>);</span><br><span class="line">  <span class="type">uint32_t</span> pad_rows1 = pads.<span class="built_in">at</span>(<span class="number">0</span>);  <span class="comment">// up</span></span><br><span class="line">  <span class="type">uint32_t</span> pad_rows2 = pads.<span class="built_in">at</span>(<span class="number">1</span>);  <span class="comment">// bottom</span></span><br><span class="line">  <span class="type">uint32_t</span> pad_cols1 = pads.<span class="built_in">at</span>(<span class="number">2</span>);  <span class="comment">// left</span></span><br><span class="line">  <span class="type">uint32_t</span> pad_cols2 = pads.<span class="built_in">at</span>(<span class="number">3</span>);  <span class="comment">// right</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// at column 0, insert a copy of pad_rows1;</span></span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">insert_rows</span>(<span class="number">0</span>, pad_rows1);</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">insert_rows</span>(<span class="keyword">this</span>-&gt;data_.n_rows, pad_rows2);</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">insert_cols</span>(<span class="number">0</span>, pad_cols1);</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">insert_cols</span>(<span class="keyword">this</span>-&gt;data_.n_cols, pad_cols2);  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Fill</span>(<span class="type">float</span> value) &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_.<span class="built_in">fill</span>(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Fill</span>(<span class="type">const</span> std::vector&lt;<span class="type">float</span>&gt; &amp;values) &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> total_elems = <span class="keyword">this</span>-&gt;data_.<span class="built_in">size</span>();</span><br><span class="line">  <span class="built_in">CHECK_EQ</span>(values.<span class="built_in">size</span>(), total_elems);</span><br><span class="line"></span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> rows = <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>();</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> cols = <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>();</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> planes = rows * cols;</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> channels = <span class="keyword">this</span>-&gt;data_.n_slices;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span>(<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; channels; i++) &#123;</span><br><span class="line">    <span class="keyword">auto</span> &amp;channel_data = <span class="keyword">this</span>-&gt;data_.<span class="built_in">slice</span>(i);</span><br><span class="line">    <span class="comment">// values.data() 返回指向作为元素存储工作的底层数组的指针</span></span><br><span class="line">    <span class="type">const</span> arma::fmat &amp;<span class="type">channel_data_t</span> = arma::<span class="built_in">fmat</span>(values.<span class="built_in">data</span>() + i * planes, <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>(), <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>());</span><br><span class="line">    channel_data = <span class="type">channel_data_t</span>.<span class="built_in">t</span>();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// 判断张量是否为空，未初始化</span></span><br><span class="line">  <span class="function"><span class="type">bool</span> <span class="title">empty</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line">  <span class="comment">// 输出张量</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Show</span><span class="params">()</span></span>;</span><br><span class="line">  <span class="comment">// 展平张量</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Flatten</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">empty</span>() <span class="type">const</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Show</span>() &#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> i = <span class="number">0</span>; i &lt; <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>(); ++i) &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(INFO) &lt;&lt; <span class="string">&quot;Channel: &quot;</span> &lt;&lt; i;</span><br><span class="line">    <span class="built_in">LOG</span>(INFO) &lt;&lt; <span class="string">&quot;\n&quot;</span> &lt;&lt; <span class="keyword">this</span>-&gt;data_.<span class="built_in">slice</span>(i);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> Tensor&lt;<span class="type">float</span>&gt;::<span class="built_in">Flatten</span>() &#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(!<span class="keyword">this</span>-&gt;data_.<span class="built_in">empty</span>());</span><br><span class="line">  <span class="type">const</span> <span class="type">uint32_t</span> size = <span class="keyword">this</span>-&gt;data_.<span class="built_in">size</span>();</span><br><span class="line">  <span class="function">arma::fcube <span class="title">linear_cube</span><span class="params">(size, <span class="number">1</span>, <span class="number">1</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="type">uint32_t</span> channel = <span class="keyword">this</span>-&gt;<span class="built_in">channels</span>();</span><br><span class="line">  <span class="type">uint32_t</span> rows = <span class="keyword">this</span>-&gt;<span class="built_in">rows</span>();</span><br><span class="line">  <span class="type">uint32_t</span> cols = <span class="keyword">this</span>-&gt;<span class="built_in">cols</span>();</span><br><span class="line">  <span class="type">uint32_t</span> index = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">uint32_t</span> c = <span class="number">0</span>; c &lt; channel; ++c) &#123;</span><br><span class="line">    <span class="type">const</span> arma::fmat &amp;matrix = <span class="keyword">this</span>-&gt;data_.<span class="built_in">slice</span>(c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">uint32_t</span> r = <span class="number">0</span>; r &lt; rows; ++r) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">uint32_t</span> c_ = <span class="number">0</span>; c_ &lt; cols; ++c_) &#123;</span><br><span class="line">        linear_cube.<span class="built_in">at</span>(index, <span class="number">0</span>, <span class="number">0</span>) = matrix.<span class="built_in">at</span>(r, c_);</span><br><span class="line">        index += <span class="number">1</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">CHECK_EQ</span>(index, size);</span><br><span class="line">  <span class="keyword">this</span>-&gt;data_ = linear_cube;</span><br><span class="line">  <span class="keyword">this</span>-&gt;raw_shapes_ = std::vector&lt;<span class="type">uint32_t</span>&gt;&#123;size&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="完整的接口定义"><a href="#完整的接口定义" class="headerlink" title="完整的接口定义"></a>完整的接口定义</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;&gt;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tensor</span>&lt;<span class="type">float</span>&gt; &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">Tensor</span><span class="params">()</span> </span>= <span class="keyword">default</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">Tensor</span><span class="params">(<span class="type">uint32_t</span> channels, <span class="type">uint32_t</span> rows, <span class="type">uint32_t</span> cols)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">Tensor</span>(<span class="type">const</span> Tensor &amp;tensor);</span><br><span class="line"></span><br><span class="line">  Tensor&lt;<span class="type">float</span>&gt; &amp;<span class="keyword">operator</span>=(<span class="type">const</span> Tensor &amp;tensor);</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">rows</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">cols</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">channels</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">uint32_t</span> <span class="title">size</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">set_data</span><span class="params">(<span class="type">const</span> arma::fcube &amp;data)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">bool</span> <span class="title">empty</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">float</span> <span class="title">index</span><span class="params">(<span class="type">uint32_t</span> offset)</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function">std::vector&lt;<span class="type">uint32_t</span>&gt; <span class="title">shapes</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function">arma::fcube &amp;<span class="title">data</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">const</span> arma::fcube &amp;<span class="title">data</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function">arma::fmat &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">const</span> arma::fmat &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel)</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">float</span> <span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col)</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">float</span> &amp;<span class="title">at</span><span class="params">(<span class="type">uint32_t</span> channel, <span class="type">uint32_t</span> row, <span class="type">uint32_t</span> col)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Padding</span><span class="params">(<span class="type">const</span> std::vector&lt;<span class="type">uint32_t</span>&gt; &amp;pads, <span class="type">float</span> padding_value)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Fill</span><span class="params">(<span class="type">float</span> value)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Fill</span><span class="params">(<span class="type">const</span> std::vector&lt;<span class="type">float</span>&gt; &amp;values)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Ones</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Rand</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Show</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Flatten</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span>:</span><br><span class="line">  std::vector&lt;<span class="type">uint32_t</span>&gt; raw_shapes_;</span><br><span class="line">  arma::fcube data_;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">TEST</span>(test_tensor, create) &#123;</span><br><span class="line">  <span class="keyword">using</span> <span class="keyword">namespace</span> kuiper_infer;</span><br><span class="line">  <span class="function">Tensor&lt;<span class="type">float</span>&gt; <span class="title">tensor</span><span class="params">(<span class="number">3</span>, <span class="number">32</span>, <span class="number">32</span>)</span></span>;</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">channels</span>(), <span class="number">3</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">rows</span>(), <span class="number">32</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">cols</span>(), <span class="number">32</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">empty</span>(), <span class="literal">false</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">TEST</span>(test_tensor, fill) &#123;</span><br><span class="line">  <span class="keyword">using</span> <span class="keyword">namespace</span> kuiper_infer;</span><br><span class="line">  <span class="function">Tensor&lt;<span class="type">float</span>&gt; <span class="title">tensor</span><span class="params">(<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>)</span></span>;</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">channels</span>(), <span class="number">3</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">rows</span>(), <span class="number">3</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">cols</span>(), <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">  std::vector&lt;<span class="type">float</span>&gt; values;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">27</span>; ++i) &#123;</span><br><span class="line">    values.<span class="built_in">push_back</span>((<span class="type">float</span>) i);</span><br><span class="line">  &#125;</span><br><span class="line">  tensor.<span class="built_in">Fill</span>(values);</span><br><span class="line">  <span class="built_in">LOG</span>(INFO) &lt;&lt; tensor.<span class="built_in">data</span>();</span><br><span class="line"></span><br><span class="line">  <span class="type">int</span> index = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> c = <span class="number">0</span>; c &lt; tensor.<span class="built_in">channels</span>(); ++c) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> c_ = <span class="number">0</span>; c_ &lt; tensor.<span class="built_in">cols</span>(); ++c_) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> r = <span class="number">0</span>; r &lt; tensor.<span class="built_in">rows</span>(); ++r) &#123;</span><br><span class="line">        <span class="built_in">ASSERT_EQ</span>(values.<span class="built_in">at</span>(index), tensor.<span class="built_in">at</span>(c, c_, r));</span><br><span class="line">        index += <span class="number">1</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">LOG</span>(INFO) &lt;&lt; <span class="string">&quot;Test1 passed!&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">TEST</span>(test_tensor, padding1) &#123;</span><br><span class="line">  <span class="keyword">using</span> <span class="keyword">namespace</span> kuiper_infer;</span><br><span class="line">  <span class="function">Tensor&lt;<span class="type">float</span>&gt; <span class="title">tensor</span><span class="params">(<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>)</span></span>;</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">channels</span>(), <span class="number">3</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">rows</span>(), <span class="number">3</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">cols</span>(), <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">  tensor.<span class="built_in">Fill</span>(<span class="number">1.f</span>); <span class="comment">// 填充为1</span></span><br><span class="line">  tensor.<span class="built_in">Padding</span>(&#123;<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>&#125;, <span class="number">0</span>); <span class="comment">// 边缘填充为0</span></span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">rows</span>(), <span class="number">5</span>);</span><br><span class="line">  <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">cols</span>(), <span class="number">5</span>);</span><br><span class="line"></span><br><span class="line">  <span class="type">int</span> index = <span class="number">0</span>;</span><br><span class="line">  <span class="comment">// 检查一下边缘被填充的行、列是否都是0</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> c = <span class="number">0</span>; c &lt; tensor.<span class="built_in">channels</span>(); ++c) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> c_ = <span class="number">0</span>; c_ &lt; tensor.<span class="built_in">cols</span>(); ++c_) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> r = <span class="number">0</span>; r &lt; tensor.<span class="built_in">rows</span>(); ++r) &#123;</span><br><span class="line">        <span class="keyword">if</span> (c_ == <span class="number">0</span> || r == <span class="number">0</span>) &#123;</span><br><span class="line">          <span class="built_in">ASSERT_EQ</span>(tensor.<span class="built_in">at</span>(c, c_, r), <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        index += <span class="number">1</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">LOG</span>(INFO) &lt;&lt; <span class="string">&quot;Test2 passed!&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2023/03/13/kuiper_infer-L2/">https://kilogrand.gitee.io/2023/03/13/kuiper_infer-L2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/kuiper-infer/">kuiper_infer</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/02/26/tuning_playbook-zh_cn/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">深度学习调优指南中文版</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/14/kuiper_infer-L3/"><img class="next-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">自制深度学习框架--导入数据</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-20</div><div class="title">自制深度学习框架--Im2Col原理与卷积层的实现</div></div></a></div><div><a href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-21</div><div class="title">自制深度学习框架--再探Tensor类并构建计算图的图关系</div></div></a></div><div><a href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-24</div><div class="title">自制深度学习框架--实现Yolov5的推理</div></div></a></div><div><a href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-22</div><div class="title">自制深度学习框架--算子的执行流程</div></div></a></div><div><a href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-23</div><div class="title">自制深度学习框架--实现ResNet网络的推理</div></div></a></div><div><a href="/2023/03/14/kuiper_infer-L3/" title="自制深度学习框架--导入数据"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-14</div><div class="title">自制深度学习框架--导入数据</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Tensor%E7%B1%BB%E6%A8%A1%E6%9D%BF"><span class="toc-number">1.</span> <span class="toc-text">Tensor类模板</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%A0%E9%87%8F"><span class="toc-number">2.</span> <span class="toc-text">张量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0"><span class="toc-number">2.1.</span> <span class="toc-text">构造函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%A0%E9%87%8F%E7%9A%84%E7%BB%B4%E5%BA%A6%E5%A4%A7%E5%B0%8F"><span class="toc-number">2.2.</span> <span class="toc-text">张量的维度大小</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%96%E6%95%B0%E6%8D%AE%E5%92%8C%E7%B4%A2%E5%BC%95"><span class="toc-number">2.3.</span> <span class="toc-text">取数据和索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E5%BC%A0%E9%87%8F"><span class="toc-number">2.4.</span> <span class="toc-text">初始化张量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%A0%E9%87%8F%E5%A1%AB%E5%85%85"><span class="toc-number">2.5.</span> <span class="toc-text">张量填充</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96"><span class="toc-number">2.6.</span> <span class="toc-text">其他</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%8C%E6%95%B4%E7%9A%84%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89"><span class="toc-number">2.7.</span> <span class="toc-text">完整的接口定义</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">使用</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>